<?php

class Model_Forum extends Forum_Db_Table
{
	protected $_name = 'forum';
    
    protected $_primary = 'id';

    /**
     * Get list of forum
     * 
     * @return array
     */
    public function getList($mode = 'enabled'){
        $query = "SELECT f.*, fg.title AS `group`
            FROM {$this->_name} AS f
            LEFT JOIN forum_group AS fg ON fg.id = f.forum_group_id";
        
        if ($mode != 'all') {
            $query .= " WHERE f.status = '{$mode}'";
        }

        return $this->getAdapter()->fetchAll($query);
    }
	
	
    /**
     * Get list of forum by forum_group id
     * 
     * @return array
     */
    public function getListGroup($id){
    	if(isset($id)){
	        $query = "SELECT *
	                  FROM {$this->_name}
					  WHERE status = 'enabled' AND forum_group_id = {$id}";
	        return $this->getAdapter()->fetchAll($query);    		
    	}
		return null;
    }
	
	public function getById($id){
		if(isset($id)){
			$query = "SELECT *
	                  FROM {$this->_name}
					  WHERE status = 'enabled' AND id = {$id}";
	
	        return $this->getAdapter()->fetchRow($query);			
		}
		return null;
	}
    
    /**
     * Add a new forum
     * 
     * @param array $data
     * @return bool
     */
    public function insert($data)
    {
        if (!isset($data['title']) || empty($data['title'])) {
            Forum_Message::getInstance()->addError('Вкажіть назву форума');
            return false;
        }
        
        $data['user_id'] = Model_User::getUserId();
        
        $this->getAdapter()->insert($this->_name, $data);
        Forum_Message::getInstance()->addSuccess('Форум успішно збережено');
        return true;
    }
	
    /**
     * Updates forum row according to $bind param
     * 
     * @param mixed $ids
     * @param array $bind Column => Value pairs
     * @return bool
     */
    public function update($ids, $bind)
    {
        if (!is_array($ids)) {
            $ids = array($ids);
        }
        
        $count = count($ids);
        $ids = implode(',', $ids);
        
        if ($this->getAdapter()->update($this->_name, $bind, array("id IN ({$ids})"))) {
            Forum_Message::getInstance()->addSuccess(sprintf('%d %s успішно оновлено', $count, (
                $count > 4 ? 'форумів' : (
                    $count > 1 ? 'форума' : 'форум' //this is wrong, and i know this :/
                )
            )));
            return true;
        } else {
            Forum_Message::getInstance()->addError('Записи не були оновлені. Спробуйте ще раз');
            return false;
        }
    }
    
    /**
     * Delete forum rows by ids
     * 
     * @param mixed $id
     * @return bool
     */
    public function delete($ids)
    {
        if (!is_array($ids)) {
            $ids = array($ids);
        }
        
        $count = count($ids);
        $ids = implode(',', $ids);
        
        if ($this->getAdapter()->delete($this->_name, "id IN ({$ids})")) {
            Forum_Message::getInstance()->addSuccess(sprintf('%d %s успішно видалено', $count, (
                $count > 4 ? 'форумів' : (
                    $count > 1 ? 'форума' : 'форум' //this is wrong, and i know this :/
                )
            )));
            return true;
        } else {
            Forum_Message::getInstance()->addError('Записи не були видалені. Спробуйте ще раз');
            return false;
        }
    }
}

?>